package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityBiddingCargoPublishOfferBinding;
import com.sgb.kjwl.model.entity.transport.CargoPublishEntity;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.utils.RangeFocusChangeListener;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.viewmodel.transport.BiddingCargoPublishOfferViewModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

/**
 * @author: 杨畔
 * @date: 2020/12/29 17:50
 * @Description: 竞价货源发布填写报价信息页面
 */
public class BiddingCargoPublishOfferActivity extends AppActivity {
    BiddingCargoPublishOfferViewModel viewModel;
    ActivityBiddingCargoPublishOfferBinding binding;
    //数据实体类
    private CargoPublishEntity mEntity;

    public static void start(Activity activity, CargoPublishEntity entity, int requestCode) {
        Intent intent = new Intent(activity, BiddingCargoPublishOfferActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(RouterKey.DATA, entity);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        viewModel = new ViewModelProvider(this).get(BiddingCargoPublishOfferViewModel.class);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_bidding_cargo_publish_offer);
        binding.setLifecycleOwner(this);
        binding.includeTitle.titleView.setText("报价要求");
        initEvent();
        initData();
    }

    private void initEvent() {
        binding.setOnClick(this);
        binding.includeTitle.ivBack.setOnClickListener(this);
        //设置输入范围，失去焦点后自动补全
        binding.etReleaseCargoRegisteredCapital.setOnFocusChangeListener(new RangeFocusChangeListener(1, 999999));
        binding.etReleaseCargoFleetSize.setOnFocusChangeListener(new RangeFocusChangeListener(1, 999999));
        binding.etReleaseCargoPerformanceBond.setOnFocusChangeListener(new RangeFocusChangeListener(10, 50000));
        //报价资格监听
        binding.cbReleaseCargoCompany.setOnCheckedChangeListener((buttonView, isChecked) ->
                mEntity.getQuotationQualification()[0] = isChecked
        );
        binding.cbReleaseCargoTeam.setOnCheckedChangeListener((buttonView, isChecked) ->
                mEntity.getQuotationQualification()[1] = isChecked
        );
        binding.cbReleaseCargoPerson.setOnCheckedChangeListener((buttonView, isChecked) ->
                mEntity.getQuotationQualification()[2] = isChecked
        );
    }

    private void initData() {
        mEntity = getIntent().getParcelableExtra(RouterKey.DATA);
        if (mEntity == null) {
            mEntity = new CargoPublishEntity();
        }
        if (mEntity.getQuotationQualification() == null) {
            mEntity.setQuotationQualification(new boolean[]{true, true, true});
        }
        syncData();
    }

    private void syncData() {
        //todo 报价资格、注册资本、车队规模、运输资质、其他要求
        binding.cbReleaseCargoCompany.setChecked(mEntity.getQuotationQualification()[0]);
        binding.cbReleaseCargoTeam.setChecked(mEntity.getQuotationQualification()[1]);
        binding.cbReleaseCargoPerson.setChecked(mEntity.getQuotationQualification()[2]);
        if (mEntity.getRegisteredCapital() != null) {
            binding.etReleaseCargoRegisteredCapital.setText(NumberUtil.trim0(mEntity.getRegisteredCapital()));
        }
        if (mEntity.getFleetSize() != null) {
            binding.etReleaseCargoFleetSize.setText(NumberUtil.trim0(mEntity.getFleetSize()));
        }
        if (!TextUtils.isEmpty(mEntity.getTransportationQualification())) {
            binding.etReleaseCargoTransportQualification.setText(mEntity.getTransportationQualification());
        }
        //履约保证金
        if (mEntity.getPerformanceBond() != null && mEntity.getPerformanceBond() != 0) {
            binding.etReleaseCargoPerformanceBond.setText(NumberUtil.trim0(mEntity.getPerformanceBond()));
        }
        if (!TextUtils.isEmpty(mEntity.getOtherRequirements())) {
            binding.etReleaseCargoOther.setText(mEntity.getOtherRequirements());
        }
    }

    @Override
    public void onClick(View v) {
        if (v == binding.includeTitle.ivBack) {
            finish();
        } else if (v == binding.tvReleaseCargoSubmit) {
            clearAllFocus();
            checkData();
        }
    }

    /**
     * 检查数据并回传
     */
    private void checkData() {
        if (mEntity == null) return;
        //todo 报价资格、注册资本、车队规模、运输资质、其他要求
        boolean atLeastOne = false;
        for (boolean b : mEntity.getQuotationQualification()) {
            if (b) {
                atLeastOne = true;
                break;
            }
        }
        if (!atLeastOne) {
            MToast.showLongToast(this, "至少选择一个报价资格");
            return;
        }
        //注册资本
        if (!TextUtils.isEmpty(binding.etReleaseCargoRegisteredCapital.getText())) {
            mEntity.setRegisteredCapital(NumberUtil.convertToDouble(binding.etReleaseCargoRegisteredCapital.getText().toString()));
        } else {
            mEntity.setRegisteredCapital(null);
        }
        //车队规模
        if (!TextUtils.isEmpty(binding.etReleaseCargoFleetSize.getText())) {
            mEntity.setFleetSize(NumberUtil.convertToInt(binding.etReleaseCargoFleetSize.getText().toString()));
        } else {
            mEntity.setFleetSize(null);
        }
        //运输资质
        if (binding.etReleaseCargoTransportQualification.getText() != null) {
            mEntity.setTransportationQualification(binding.etReleaseCargoTransportQualification.getText().toString());
        } else {
            mEntity.setTransportationQualification(null);
        }
        //履约保证金
        if (!TextUtils.isEmpty(binding.etReleaseCargoPerformanceBond.getText())) {
            mEntity.setPerformanceBond(NumberUtil.convertToDouble(binding.etReleaseCargoPerformanceBond.getText().toString()));
        } else {
            mEntity.setPerformanceBond(null);
        }
        //其他要求
        if (binding.etReleaseCargoOther.getText() != null) {
            mEntity.setOtherRequirements(binding.etReleaseCargoOther.getText().toString());
        } else {
            mEntity.setOtherRequirements(null);
        }
        Intent intent = new Intent();
        intent.putExtra(RouterKey.DATA, mEntity);
        setResult(RESULT_OK, intent);
        finish();
    }
}
